import sys
from typing import Any, TextIO

from .metrics_logger import MetricsLogger
from .metrics import MetricOrValue, get_metric_scalar_value


class FileMetricsLogger(MetricsLogger):
    def __init__(self, file: TextIO):
        self.file: Any = file

    def log(self, metrics: dict[str, MetricOrValue]):
        print(f"\nSummary:", file=self.file)
        for name, metric in metrics.items():
            value = get_metric_scalar_value(metric)
            if value is not None:
                print(f"\t{name}={value}", file=self.file)


class StdOutMetricsLogger(FileMetricsLogger):
    def __init__(self):
        super().__init__(sys.stdout)


class StdErrMetricsLogger(FileMetricsLogger):
    def __init__(self):
        super().__init__(sys.stderr)
